{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 19 字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.【填空】有两个列表 a = [\"name\",\"age\",\"sex\"]，b = [\"jonh\",\"23\",\"M\"],请用一个语句将这两个列表转换成字典，其中列表a中的元素为“键”，列表b中的元素为“值”。（ ）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：c = dict(zip(a,b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.【填空】定义一个新字典如下，用print输出dict1的结果是：（ ）  \n",
    "`dict1 = {1:\"z\",2:\"a\",1:3}`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：{1: 3, 2: 'a'}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.【填空】写出下列Python语句的运行结果。（ ）  \n",
    "`\n",
    "def func(args1,*args2,**args3,):\n",
    "    print(args3)\n",
    "func(\"a\",1,x1=2,x2=3)`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：{'x1': 2, 'x2': 3}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.【判断】下面对字典d的定义是正确的。（ ）  \n",
    "`d = {[\"a\",\"b\"]:[1,2],\"c\":3}`\n",
    "\n",
    "A.√\n",
    "B.×"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：B"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5.【编程】有如下字典，按照要求实现每一个功能。  \n",
    "`dict = {\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\"}`  \n",
    "\n",
    "（1）请循环遍历出所有的key和value。  \n",
    "（2）请在字典中增加一个键值对,\"k4\":\"v4\"，输出添加后的字典。  \n",
    "（3）修改k1的值为v5，输出修改后的字典。  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "参考答案：  \n",
    "`\n",
    "dict = {\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\"}\n",
    "for k in dict:\n",
    "    print(k,dict[k])\n",
    "dict[\"k4\"] = \"v4\"\n",
    "print(\"dict新增'k4':'v4'后的值: \",dict)\n",
    "dict[\"k1\"] = \"v5\"\n",
    "print(\"dict修改'k1'后的值: \",dict)`"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
